package com.ls.fw.data.hbase.utils;

import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ls.fw.data.enums.FieldType;

public abstract class DataTypeConvertUtil {

	protected static Logger logger = LoggerFactory.getLogger(DataTypeConvertUtil.class);

	public static byte[] dataConvertByte(Object object)
			throws Exception {
		byte[] value = null;
		if(object != null && !(object.toString()).equals("")){
			if(object.getClass().equals(int.class)){
				value = Bytes.toBytes(Integer.parseInt(object.toString()));
			}else if(object.getClass().equals(long.class)){
				value = Bytes.toBytes(Long.parseLong(object.toString()));
			}else if(object.getClass().equals(boolean.class)){
				value = Bytes.toBytes(Boolean.valueOf(object.toString()));
			}else if(object.getClass().equals(double.class)){
				value = Bytes.toBytes(Double.parseDouble(object.toString()));
			}else if(object.getClass().equals(short.class)){
				value = Bytes.toBytes(Short.parseShort(object.toString()));
			}else if(object.getClass().equals(byte.class)){
				value = Bytes.toBytes(Byte.parseByte(object.toString()));
			}else if(object.getClass().equals(float.class)){
				value = Bytes.toBytes(Float.parseFloat(object.toString()));
			}else{
				value = Bytes.toBytes(object.toString());
			}
		}
		return value;
	}
	/**
	 * 
	 * object转换成byte数据
	 * @param value
	 * @param fieldType
	 * @return
	 */
	public static byte[] dataConvertByte(Object object, FieldType fieldType)
			throws Exception {
		byte[] value = null;
		if(object != null && !(object.toString()).equals("")){
			switch (fieldType) {
			case longx:
				value = Bytes.toBytes(Long.parseLong(object.toString()));
				break;
			case booleanx:
				value = Bytes.toBytes(Boolean.valueOf(object.toString()));
				break;
			case doublex:
				value = Bytes.toBytes(Double.parseDouble(object.toString()));
				break;
			case integer:
				value = Bytes.toBytes(Integer.parseInt(object.toString()));
				break;
			case shortx:
				value = Bytes.toBytes(Short.parseShort(object.toString()));
				break;	
			case bytex:
				value = Bytes.toBytes(Byte.parseByte(object.toString()));
				break;	
			case floatx:
				value = Bytes.toBytes(Float.parseFloat(object.toString()));
				break;	
			default:
				value = Bytes.toBytes(object.toString());
				break;
			}
		}
		return value;
	}
	
	/**
	 * 
	 * byte转换成object数据
	 * @param value
	 * @param fieldType
	 * @return
	 */
	public static Object byteConvertObject(byte[] value, FieldType fieldType) {
		Object ret = null;
		if (null == value || "".equals(Bytes.toString(value))) {
			return "";
		}
		switch (fieldType) {
		case longx:
			ret = Bytes.toLong(value);
			break;
		case booleanx:
			ret = Bytes.toBoolean(value);
			break;
		case doublex:
			ret = Bytes.toDouble(value);
			break;
		case integer:
			ret = Bytes.toInt(value);
			break;
		case shortx:
			ret = Bytes.toShort(value);
			break;	
		case floatx:
			ret = Bytes.toFloat(value);
			break;	
		default:
			ret = Bytes.toString(value);
			break;
		}
		return ret;
	}
}
